package com.qxsx.midi.adapter;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.qxsx.midi.R;
import com.qxsx.midi.activity.home.CourseDetailsActivity;
import com.qxsx.midi.activity.me.MeMessageActivity;
import com.qxsx.midi.model.Msg;
import com.qxsx.midi.model.Order;
import com.qxsx.midi.model.Teacher;
import com.qxsx.midi.utils.CustomProgress;
import com.qxsx.midi.utils.MyNet;
import com.qxsx.midi.utils.callBackJsonArray;
import com.qxsx.midi.view.ZListView.BaseSwipeAdapter;
import com.qxsx.midi.view.ZListView.DragEdge;
import com.qxsx.midi.view.ZListView.ShowMode;
import com.qxsx.midi.view.ZListView.SimpleSwipeListener;
import com.qxsx.midi.view.ZListView.ZSwipeItem;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.w3c.dom.Text;

import java.util.ArrayList;
import java.util.List;

public class MeMsgListAdapter extends BaseSwipeAdapter {
    private CustomProgress customProgress;

    private Context context;
    private ArrayList<Msg> arrayList = new ArrayList<>();
    private LayoutInflater listContainer; // 视图容器
    private int myPosition;
    private Activity activity;

    public MeMsgListAdapter(Context context, ArrayList<Msg> arrayList, Activity activity) {
        super();
        this.context = context;
        listContainer = LayoutInflater.from(context); // 创建视图容器并设置上下文
        this.arrayList = arrayList;
        this.activity = activity;

    }

    @Override
    public int getCount() {
        return arrayList.size();
    }

    @Override
    public Object getItem(int position) {
        return arrayList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getSwipeLayoutResourceId(int position) {
        return R.id.swipe_item;
    }

    @Override
    public View generateView(int position, ViewGroup parent) {
        return listContainer.inflate(R.layout.item_list_my_msg, parent, false);
    }

    @Override
    public void fillValues(final int position, View convertView) {

        final ZSwipeItem swipeItem = (ZSwipeItem) convertView.findViewById(R.id.swipe_item);
        final Msg msg = arrayList.get(position);
        ViewHolder viewHolder = new ViewHolder();

        viewHolder.msg_title = (TextView) convertView.findViewById(R.id.msg_title);
        viewHolder.msg_date = (TextView) convertView.findViewById(R.id.msg_date);
        viewHolder.msg_content = (TextView) convertView.findViewById(R.id.msg_content);

        viewHolder.msg_title.setText(msg.getTitle());
        viewHolder.msg_date.setText(msg.getDate());
        viewHolder.msg_content.setText(msg.getContent());

        LinearLayout ll = (LinearLayout) convertView.findViewById(R.id.ll);


        convertView.setTag(viewHolder);


        swipeItem.setShowMode(ShowMode.PullOut);
        swipeItem.setDragEdge(DragEdge.Right);


        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent it = new Intent(activity, MeMessageActivity.class);
                it.putExtra("id", msg.getId());//传递商家ID
                activity.startActivity(it);
            }
        });

        swipeItem.addSwipeListener(new SimpleSwipeListener() {

            @Override
            public void onOpen(ZSwipeItem layout) {
                myPosition = position;
            }

            @Override
            public void onClose(ZSwipeItem layout) {
                myPosition = position;
            }

            @Override
            public void onStartOpen(ZSwipeItem layout) {
                myPosition = position;
            }

            @Override
            public void onStartClose(ZSwipeItem layout) {
                myPosition = position;
            }

            @Override
            public void onHandRelease(ZSwipeItem layout, float xvel, float yvel) {
                myPosition = position;
            }

            @Override
            public void onUpdate(ZSwipeItem layout, int leftOffset, int topOffset) {
            }
        });

        ll.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                customProgress = CustomProgress.show(activity, "正在删除....", true, null);

                JSONArray array = new JSONArray();
                JSONObject object = new JSONObject();
                try {
                    object.put("messge_id", arrayList.get(position).getId());
                    array.put(object);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                MyNet.doPostJsonArray("111", array, new callBackJsonArray() {
                    @Override
                    public void run(Object code, Object message, JSONArray array) {
                        if ("200".equals(code.toString())) {
                            arrayList.remove(position);
                            swipeItem.close(false);
                            notifyDataSetChanged();
                        } else {
                            Log.e("code", code.toString());
                            Log.e("message", message.toString());
                        }
                        customProgress.dismiss();
                    }
                });

            }
        });
    }

    public void AddData(List<Msg> list) {
        for (Msg msg : list) {
            arrayList.add(msg);
        }
        notifyDataSetChanged();
    }

    public void RemoveAll() {
        if (arrayList != null) {
            this.arrayList.clear();
            notifyDataSetChanged();
        }
    }

    static class ViewHolder {
        TextView msg_title;
        TextView msg_date;
        TextView msg_content;
    }


}
